package userapi

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"serviceApi/formData/userApi"
	"serviceApi/service/userSrv"
)

func UserRegister(c *gin.Context) {
	var r userApi.UserRegister
	if err := c.ShouldBindJSON(&r); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	register, err := userSrv.UserRegister(r)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
			"code":  http.StatusInternalServerError,
			"data":  nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"data":    register,
		"message": "success",
	})

}
func UserLogin(c *gin.Context) {
	var r userApi.UserLogin
	if err := c.ShouldBindJSON(&r); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	login, err := userSrv.UserLogin(r)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"data":    login,
		"message": "登录成功",
	})
}
